import axios from 'axios';
import {
    Message
} from 'element-ui'
import store from '../vuex/store'

import Qs from 'qs'
// import Vue from 'vue'
// import VueAxios from 'vue-axios'
// Vue.use(VueAxios, axios);
var _ = require('lodash');

class base {
    option = {
        herders: {
            "content-type": "application/json"
        },
        timeout: 10000
    }
}
class http extends base {
    get(url, params = {}, headers = {}) {
        return this.request('get', url, params, headers)
    }
    post(url, params = {}, headers = {}) {
        return this.request('post', url, params, headers)
    }
    put(url, params = {}, headers = {}) {
        return this.request('put', url, params, headers)
    }
    delete(url, params = {}, headers = {}) {
        return this.request('delete', url, params, headers)
    }
    /**
     * mothed 请求方法
     * url 请求url
     * params 请求参数
     * haerders 请求头，默认用option
     */
    request(mothed, url, params, headers) {

        return new Promise((resolve, reject) => {
            var head = {};
            head = _.cloneDeep(this.option);
            if (head['content-type']) {
                head['content-type'] = headers['content-type']
            }


            axios.interceptors.request.use(
                config => {
                    //放入token和账套id
                    let token = store.state.token_mark;
                    console.log('token request', token)
                    config.headers.token_mark = token;
                    config.headers.set_book_id = store.state.accountSet.setBookId;
                    return config;
                },
                error => {
                    console.log(error);
                    store.commit('changeToken', '');
                    window.sessionStorage.removeItem('store');
                    return Promise.reject();
                }
            );


            if (mothed == 'post') {
                axios.post(url, params, head)
                    .then(res => {
                        console.log(res, 'request')
                        if (res.status == 200) {
                            if (res.headers.token_mark) {
                                store.commit('changeToken', res.headers.token_mark);
                                console.log('res.headers.token_mark', res.headers.token_mark)
                            }
                            console.log(res, 'res')
                            return resolve(res)
                        } else {
                            store.commit('changeToken', '');
                            window.sessionStorage.removeItem('store');
                            return reject(res)
                        }
                    })
                    .catch(err => {
                        console.log(err, 'httperr')
                        store.commit('changeToken', '');
                        window.sessionStorage.removeItem('store');
                        return reject(err)
                    })
            } else if (mothed == 'get') {
                axios.get(url, {
                        params: params
                    }, head)
                    .then(res => {
                        console.log(res, 'request')
                        if (res.status == 200) {
                            if (res.headers.token_mark) {
                                store.commit('changeToken', res.headers.token_mark);
                                console.log('res.headers.token_mark', res.headers.token_mark)
                            }
                            return resolve(res)
                        } else {
                            store.commit('changeToken', '');
                            window.sessionStorage.removeItem('store');
                            return reject(res.data.message)
                        }
                    })
                    .catch(err => {
                        console.log(err, 'httperr')
                        store.commit('changeToken', '');
                        window.sessionStorage.removeItem('store');
                        return reject(err)
                    })
            } else if (mothed == 'put') {
                axios.put(url, params, head)
                    .then(res => {
                        console.log(res, 'request')
                        if (res.status == 200) {
                            if (res.headers.token_mark) {
                                store.commit('changeToken', res.headers.token_mark);
                                console.log('res.headers.token_mark', res.headers.token_mark)
                            }
                            return resolve(res)
                        } else {
                            store.commit('changeToken', '');
                            window.sessionStorage.removeItem('store');
                            return reject(res)
                        }
                    })
                    .catch(err => {
                        console.log(err, 'httperr')
                        store.commit('changeToken', '');
                        window.sessionStorage.removeItem('store');
                        return reject(err)
                    })
            } else if (mothed == 'delete') {
                // delete的参数都加到url上
                axios.delete(url, {
                        data: params
                    }, head)
                    .then(res => {
                        console.log(res, 'delete, request')
                        if (res.status == 200) {
                            if (res.headers.token_mark) {
                                store.commit('changeToken', res.headers.token_mark);
                                console.log('res.headers.token_mark', res.headers.token_mark)
                            }
                            return resolve(res)
                        } else {
                            store.commit('changeToken', '');
                            window.sessionStorage.removeItem('store');
                            return reject(res)
                        }
                    })
                    .catch(err => {
                        console.log(err, 'delete catch')
                        store.commit('changeToken', '');
                        window.sessionStorage.removeItem('store');
                        return reject(err)
                    })
            }
            console.log(mothed, url, params, headers)

        })
    }

}


export default new http()